<?php

namespace App\Http\Controllers;

use App\Models\Safe;
use App\Models\SafeInspect;
use App\Models\TempInspect;
use App\Services\SafeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SafeController extends Controller
{
    protected $SafeService;
    public function __construct(SafeService $SafeService)
    {
        $this->SafeService = $SafeService;
    }

    public function index(Request $Request) {
        $data = $this->SafeService->get('');
        if($Request->id){
            $data = $data->where('person_id',$Request->id);
        }
        $data = $data->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function statistics(Request $Request) {
        $data = Safe::with('type')->with('inspect')->orderBy('created_at','desc')->get();
        $data = $data->groupBy('floor');
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function complete(Request $Request) {
        $date = Date('Y-m-d H:i:s',time());
        $sql = SafeInspect::with('safe')->orderBy('created_at','desc');
        if($Request->type == 'today'){
            $sql->whereDate('created_at','<=',$date)->whereDate('created_at','>=',$date);
        }
        $data = $sql->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request)
    {
        $data = Safe::with('type')->with('inspect')->find($Request->id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新数据。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $Request)
    {
        $POSTONLY = SafeInspect::POSTONLY;
        $input = $Request->only($POSTONLY);
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        
        $data = SafeInspect::create($res);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = SafeInspect::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        $data = SafeInspect::where('id',$input['id'])->update($res);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function temps(Request $Request) {
        $type = $Request->type ?? 'safe';
        $data = TempInspect::where('type',$type)->with('person')->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function tempShow(Request $Request) {
        $data = TempInspect::with('person')->where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
    
    public function insertTemp(Request $Request)
    {
        $POSTONLY = TempInspect::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['push_id'] = Auth::user()->id;
        $data = $this->SafeService->insertTemp($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    
}
